package rule

import "strings"

func init() {
	Register(&booleanRule{})
}

type booleanRule struct{}

func (b *booleanRule) Name() string {
	return "boolean"
}

var boolMap = map[string]struct{}{
	"1":     {},
	"true":  {},
	"on":    {},
	"yes":   {},
	"":      {},
	"0":     {},
	"false": {},
	"off":   {},
	"no":    {},
}

func (b *booleanRule) Check(in *RuleInput) (err error) {
	if in.Value != nil {
		switch in.Value.(type) {
		case bool:
			return
		case string:
			if _, ok := boolMap[strings.ToLower(in.Value.(string))]; ok {
				return
			}
		}
	}
	return in.Error()
}
